﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Windows.Forms;

namespace System
{
    public class CheckUpdate
    {

        public void StartNew(string oldFile, string newFile)
        {
            const string BatFormat = @"
@echo off
echo -------------------------------------------------------------------------------------
echo -----------------------------正在移动刚下载的更新文件，请勿关闭！----------------------
echo -------------------------------------------------------------------------------------
del ""{0}""
move ""{1}"" ""{0}""
start """" ""{0}""
rem pause
rem del %0
exit
";
            var tmpFile = Path.Combine(Path.GetTempPath(), Application.ProductName + Guid.NewGuid().ToString("N") + ".bat");
            var bat = string.Format(BatFormat, oldFile, newFile);
            File.WriteAllText(tmpFile, bat,Encoding.GetEncoding("gbk"));
            // MessageBox.Show(tmpFile);
            var pro = Process.Start(tmpFile);
            //pro.Close();
            pro.WaitForExit(0);
        }

        public string DownloadNewFile(string fileUrl)
        {
            var tmpFile = Path.GetTempFileName();
            using (var client = new WebClient())
            {
                client.DownloadFile(fileUrl, tmpFile);
            }
            return tmpFile;
        }

    }
}
